package com.ruoyi.bbsAdmin.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.ruoyi.bbsAdmin.domain.vo.PlateTree;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bbsAdmin.mapper.PlateMapper;
import com.ruoyi.bbsAdmin.domain.Plate;
import com.ruoyi.bbsAdmin.service.IPlateService;

import javax.annotation.Resource;

/**
 * 板块Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-04-01
 */
@Service
public class PlateServiceImpl implements IPlateService 
{
    @Resource
    private PlateMapper plateMapper;


    @Autowired
    private ISysRoleService roleService;

    /**
     * 查询板块
     * 
     * @param plateid 板块主键
     * @return 板块
     */
    @Override
    public Plate selectPlateByPlateid(String plateid)
    {
        return plateMapper.selectPlateByPlateid(plateid);
    }

    /**
     * 查询板块列表
     * 
     * @param plate 板块
     * @return 板块
     */
    @Override
    public List<Plate> selectPlateList(Plate plate)
    {
        return plateMapper.selectPlateList(plate);
    }
    @Override
    public List<PlateTree> selectPlateTree(PlateTree plateTree)
    {
        return plateMapper.selectPlateTree (plateTree);
    }

    /**
     * 新增板块
     * 
     * @param plate 板块
     * @return 结果
     */
    @Override
    public int insertPlate(Plate plate)
    {
        return plateMapper.insertPlate(plate);
    }

    /**
     * 修改板块
     * 
     * @param plate 板块
     * @return 结果
     */
    @Override
    public int updatePlate(Plate plate)
    {
        return plateMapper.updatePlate(plate);
    }

    /**
     * 批量删除板块
     * 
     * @param plateids 需要删除的板块主键
     * @return 结果
     */
    @Override
    public int deletePlateByPlateids(String[] plateids)
    {
        return plateMapper.deletePlateByPlateids(plateids);
    }

    /**
     * 删除板块信息
     * 
     * @param plateid 板块主键
     * @return 结果
     */
    @Override
    public int deletePlateByPlateid(String plateid)
    {
        return plateMapper.deletePlateByPlateid(plateid);
    }

    /**
     * 角色权限
     * @param plate
     * @return
     */
    @Override
    public List<Plate> selectPlateListByRole(Plate plate)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<SysRole> roles = user.getRoles();
        Set<String> rolestrs = new HashSet<>();
        for (SysRole r:roles
             ) {
            rolestrs.add(r.getRoleKey());
        }
        if (rolestrs.contains("admin")){
            return plateMapper.selectPlateList(plate);
        }else {
            // 存在多个校园，sql
            List<String> schoolIds = new ArrayList<>();
            for (String rolestr: rolestrs
                 ) {
                if (rolestr.contains("xygly")){
                    String schoolid = rolestr.replace("xygly","");
                    if (!"".equals(schoolid)){
                        schoolIds.add(rolestr.replace("xygly",""));
                    }

                }
            }
            return plateMapper.selectPlateListByRole(plate,schoolIds);

        }

    }
}
